package com.qiguliuxing.dts.service.base;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.qiguliuxing.dts.common.type.MerchantApproveEnum;
import com.qiguliuxing.dts.db.bean.DtsMerchantApplyVo;
import com.qiguliuxing.dts.db.bean.MerchantApproveParam;
import com.qiguliuxing.dts.db.dao.DtsAdminMapper;
import com.qiguliuxing.dts.db.dao.DtsBrandMerchantMapper;
import com.qiguliuxing.dts.db.dao.DtsMerchantApplyMapper;
import com.qiguliuxing.dts.db.dao.ex.DtsMerchantApplyMapperEx;
import com.qiguliuxing.dts.db.domain.*;
import com.qiguliuxing.dts.service.notify.NotifyService;
import com.qiguliuxing.dts.service.seckill.constant.MerchantEmailConstant;
import com.qiguliuxing.dts.service.system.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DtsMerchantService {


	@Resource
	private DtsMerchantApplyMapperEx merchantApplyExMapper;

	@Resource
	private DtsMerchantApplyMapper merchantApplyMapper;

	@Resource
	private DtsBrandMerchantMapper brandMerchantMapper;

	@Resource
	private DtsAdminMapper adminMapper;

	@Autowired
	private NotifyService notifyService;


	public List<DtsMerchantApplyVo> queryMerchantApply(String merchantName, String shopName, Integer approveStatus, Integer page, Integer limit) {
		Map<String,Object> params = Maps.newHashMap();
		params.put("merchantName",merchantName);
		params.put("shopName",shopName);
		params.put("approveStatus",approveStatus);
		PageHelper.startPage(page,limit);
		return merchantApplyExMapper.queryMerchantApply(params);
	}

	public void approveMerchantApply(MerchantApproveParam approve) {
		String emailContent;
		DtsAdmin admin =  adminMapper.selectByPrimaryKey(approve.getAdminId());
		DtsMerchantApplyExample example = new DtsMerchantApplyExample();
		DtsMerchantApplyExample.Criteria criteria = example.createCriteria();
		criteria.andAdminIdEqualTo(admin.getId());
		DtsMerchantApply apply = merchantApplyMapper.selectOneByExample(example);

		if(Objects.equals(approve.getIsAdopt(), MerchantApproveEnum.ADOPT.getValue())){
			//审核通过
			approve.setOpinion("审批通过");
			//1添加品牌信息
			DtsBrandMerchant brandMerchant = new DtsBrandMerchant();
			brandMerchant.setName(apply.getShopName());
			brandMerchant.setDefaultCategoryId(apply.getCategoryId());
			brandMerchant.setAddTime(LocalDateTime.now());
			brandMerchant.setAdminId(admin.getId());
			brandMerchantMapper.insertSelective(brandMerchant);

			DtsBrandMerchantExample brandExample = new DtsBrandMerchantExample();
			DtsBrandMerchantExample.Criteria brandCriteria = brandExample.createCriteria();
			brandCriteria.andAdminIdEqualTo(admin.getId());
			DtsBrandMerchant merchantBrand = brandMerchantMapper.selectOneByExample(brandExample);
			admin.setDeleted(Boolean.FALSE);
			admin.setBrandId(merchantBrand.getId());
			adminMapper.updateByPrimaryKeySelective(admin);
			emailContent = this.getSuccessApproveEmailContent(SystemConfig.getMallName(),apply.getShopName(),SystemConfig.getMerchantLink());
		}else{
			//审核不通过
			merchantApplyExMapper.merchantNotAdopt(admin.getId());
			emailContent = this.getNoSucApproveEmailContent(SystemConfig.getMallName(),apply.getShopName(),SystemConfig.getMerchantLink(),approve.getOpinion());
		}
		apply.setApproveOpinion(approve.getOpinion());
		merchantApplyMapper.updateByPrimaryKeySelective(apply);

		//邮件通知
		notifyService.notifySslMail("商户入驻审核通知",emailContent,new String[]{admin.getMail()});
	}

	private String getSuccessApproveEmailContent(String mallName,String shopName,String merchantLink){
		Map<String,Object> params = Maps.newHashMap();
		params.put("mallName",mallName);
		params.put("shopName",shopName);
		params.put("merchantLink",merchantLink);
		return MerchantEmailConstant.getEmailContent(params,MerchantEmailConstant.SUCCESS_EMAIL_TEMPLATE);
	}

	private String getNoSucApproveEmailContent(String mallName,String shopName,String merchantLink,String opinion){
		Map<String,Object> params = Maps.newHashMap();
		params.put("mallName",mallName);
		params.put("shopName",shopName);
		params.put("merchantLink",merchantLink);
		params.put("approveOpinion",opinion);
		return MerchantEmailConstant.getEmailContent(params,MerchantEmailConstant.FAIL_EMAIL_TEMPLATE);
	}

}
